package com.zust.zst.controller.studentstatus.util;

import com.zust.zst.common.vo.login.LoginVO;
import com.zust.zst.token.GlobalTokenManagement;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Objects;

/**
 * @author ldnb
 * @date 2022/12/01 17:16
 */
public class NowAdminInfo {

    /*private static final String TOKEN = "token";

    public static int getUserUid() {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();
            if (key.equals(TOKEN)) {
                if (request.getHeader(key) != null && !Objects.equals(request.getHeader(key), "")) {
                    String token = request.getHeader(key);
                    LoginVO loginVO = GlobalTokenManagement.tokenMap.get(token);
                    return loginVO.getUid();
                }
            }
        }
        return 0;
    }*/

    public static int getUserUid2() {
        HttpServletRequest request = ((ServletRequestAttributes)
                Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String token = request.getHeader("token");
        LoginVO loginVO = GlobalTokenManagement.tokenMap.get(token);
        return loginVO.getUid();
    }
}
